local extension = Package("luming_king")
extension.extensionName = "luming"
extension.game_modes_whitelist = {"lm_mode"}
Fk:loadTranslationTable{
  ["luming_king"] = "鹿鸣-王道",
}

local U = require "packages/utility/utility"
local LM = require "packages/luming/LM"

--------------------------------------------------刘备-------------------------------------------------------------
local lm_lbjieyi = fk.CreateTriggerSkill{
  name = "lm_lbjieyi",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 3

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    LM.findServants(player, { "lm__svt_guanyu", "lm__svt_zhangfei" }, self.name)
  end
}
local lm_zhenzhao = fk.CreateActiveSkill{
  name = "lm_zhenzhao",
  prompt = function()
    local not_less = #LM.getRemoveableSvts(Self) > 0 and Self:getMark("_lm_zhenzhao-phase") % 2 == 0
    local not_full = LM.canFindNewSvt(Self) and Self:getMark("_lm_zhenzhao-phase") // 2 == 0
    if not_less and not_full then
      return "#lm_zhenzhao"
    elseif not_less then
      return "#lm_zhenzhao_full"
    elseif not_full then
      return "#lm_zhenzhao_less"
    end
    return ""
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(self.name,Player.HistoryPhase) < 2
  end,
  target_filter = Util.FalseFunc,
  min_card_num = function(self)
    if #LM.getRemoveableSvts(Self) == 0 or Self:getMark("_lm_zhenzhao-phase") % 2 ~= 0 then
      return 1
    end
    return 0
  end,
  max_card_num = function(self)
    if not LM.canFindNewSvt(Self) or Self:getMark("_lm_zhenzhao-phase") // 2 ~= 0 then
      return 0
    end
    return 1
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and LM.canFindNewSvt(Self) and not Self:prohibitDiscard(to_select)
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = effect.cards
    if #cards > 0 then
      room:addPlayerMark(player, "_lm_zhenzhao-phase", 2)
      room:throwCard(cards, self.name, player, player)
      LM.findServantsByChc(player, 1, 1, 3, self.name)
    else
      room:addPlayerMark(player, "_lm_zhenzhao-phase", 1)
      LM.removeServantsByChc(player, 1, nil, player, nil, self.name)
      room:drawCards(player,1,self.name)
    end
  end
}
local lm__lord_liubei = General(extension,"lm__lord_liubei","lm_k__wang",5)
-- lm__lord_liubei.servant = "2/3"
-- LM.setlordServantInfo("lm__lord_liubei",lm__lord_liubei.servant)
lm__lord_liubei:addSkill(lm_lbjieyi)
lm__lord_liubei:addSkill(lm_zhenzhao)
Fk:loadTranslationTable{
  ["lm__lord_liubei"] = "刘备",
  ["@[lm_servants]&lm_lbjieyi"] = "将池",
  ["@[lm_servants]lm_lbjieyi"] = "将领",
  ["lm_lbjieyi"] = "结义",
  [":lm_lbjieyi"] = "军力技，你的初始将领上限为3。游戏开始时，你获得关羽、张飞。",
  ["lm_zhenzhao"] = "征召",
  [":lm_zhenzhao"] = "出牌阶段各限一次，你可以弃置一张牌并发现一名将领，或移除一名将领并摸一张牌。",
  ["#lm_zhenzhao_less"] = "你可以弃置一张牌并发现一名将领",
  ["#lm_zhenzhao_full"] = "你可以移除一名将领并摸一张牌",
  ["#lm_zhenzhao"] = "你可以弃置一张牌并发现一名将领，或移除一名将领并摸一张牌",
}
--黄忠
local lm_liegong = fk.CreateTriggerSkill{
  name = "lm_liegong",
  anim_type = "offensive",
  events = {fk.CardUsing},
  can_trigger = function (self, event, target, player, data)
    return player:hasSkill(self) and player == target and data.card and data.card.trueName == "slash"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    data.disresponsiveList = table.map(room.alive_players, Util.IdMapper)
  end,
}
local lm_liegong_slash = fk.CreateTargetModSkill{
  name = "#lm_liegong_slash",
  bypass_distances =  function(self, player, skill)
    return player:hasSkill(lm_liegong) and skill.trueName == "slash_skill"
  end,
}
local lm__svt_huangzhong = General(extension,"lm__svt_huangzhong","lm_k__wang",0)
lm__svt_huangzhong.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_huangzhong")
lm_liegong:addRelatedSkill(lm_liegong_slash)
lm__svt_huangzhong:addSkill(lm_liegong)
Fk:loadTranslationTable{
  ["lm__svt_huangzhong"] = "黄忠",
  ["#lm_liegong_slash"] = "烈弓",
  ["lm_liegong"] = "烈弓",
  [":lm_liegong"] = "你使用【杀】无距离限制且不可响应。",
}
--赵云
local lm_longdan = fk.CreateTriggerSkill{
  name = "lm_longdan",
  anim_type = "drawcard",
  events = {fk.AfterCardsMove},
  can_trigger = function(self, event, target, player, data)
    if not (player:hasSkill(self) and player:isKongcheng() and player:usedSkillTimes(self.name, Player.HistoryTurn) == 0) then return false end
    for _, move in ipairs(data) do
      if move.from == player.id then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand then
            return true
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    player:drawCards(2, self.name)
  end,
}
local lm__svt_zhaoyun = General(extension,"lm__svt_zhaoyun","lm_k__wang",0)
lm__svt_zhaoyun.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_zhaoyun")
lm__svt_zhaoyun:addSkill(lm_longdan)
Fk:loadTranslationTable{
  ["lm__svt_zhaoyun"] = "赵云",
  ["lm_longdan"] = "龙胆",
  [":lm_longdan"] = "每回合限一次，当你失去最后的手牌后，你可以摸两张牌。",
}
--法正
local lm_fzyiji = fk.CreateViewAsSkill{
  name = "lm_fzyiji",
  anim_type = "offensive",
  pattern = "slash",
  card_filter = function(self, to_select, selected)
    if #selected > 1 then return end
    if #selected == 0 then return true end
    if #selected == 1 then return Fk:getCardById(selected[1]).number ~= Fk:getCardById(to_select).number end
  end,
  view_as = function(self, cards)
    local c = Fk:cloneCard("slash")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
  before_use = function(self, player, use)
    local room = player.room
    local targets = TargetGroup:getRealTargets(use.tos)
    if #targets == 0 then return end
    if not use.card or use.card.skillName ~= self.name or #use.card.subcards ~= 2 then return end
    local marknums = {}
    for _,cid in ipairs(use.card.subcards) do
      local card = Fk:getCardById(cid)
      table.insert(marknums,card.number)
    end
    table.sort(marknums)
    if #marknums == 0 then return end
    local markstr = "lm_fzyiji_prohiuse"
    for _,pid in ipairs(targets) do
      local tar = room:getPlayerById(pid)
      room:setPlayerMark(tar,markstr,marknums)
    end
  end,
  enabled_at_play = function(self, player)
    return Self:usedSkillTimes(self.name,Player.HistoryTurn) == 0
  end,
  enabled_at_response = function(self, player, response)
    return not response and Self:usedSkillTimes(self.name,Player.HistoryTurn) == 0
  end,
}
local lm_yijiprohibit = fk.CreateProhibitSkill{
  name = "#lm_fzyijiprohibit",
  prohibit_use = function(self, player, card)
    local mark = player:getTableMark("lm_fzyiji_prohiuse")
    if not (card or card.trueName == "jink") then return end
    if #mark ~= 0 and (card.number <= mark[1] or card.number >= mark[2]) then return true end
  end,
}
local lm_yijicardused = fk.CreateTriggerSkill{
  name = "#lm_fzyijicardused",
  anim_type = "offensive",
  refresh_events = {fk.CardUseFinished},
  can_refresh = function (self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.card and data.card.skillName == "lm_fzyiji"
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
    local markstr = "lm_fzyiji_prohiuse"
    for _,pr in ipairs(room:getOtherPlayers(player)) do
      if pr:getMark(markstr) ~= 0 then
        room:setPlayerMark(pr,markstr,0)
      end
    end
  end
}
local lm__svt_fazheng = General(extension,"lm__svt_fazheng","lm_k__wang",0)
lm__svt_fazheng.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_fazheng")
lm_fzyiji:addRelatedSkill(lm_yijiprohibit)
lm_fzyiji:addRelatedSkill(lm_yijicardused)
lm__svt_fazheng:addSkill(lm_fzyiji)
Fk:loadTranslationTable{
  ["lm__svt_fazheng"] = "法正",
  ["#lm_fzyijiprohibit"] = "翼击",
  ["#lm_fzyijicardused"] = "翼击",
  ["lm_fzyiji"] = "翼击",
  [":lm_fzyiji"] = "回合技，你可以将两张点数不同的牌当【杀】使用，目标不能使用点数不在二者之间的【闪】直到此【杀】结算结束。",
}
--张飞
local lm_paoxiao = fk.CreateTriggerSkill{
  name = "lm_paoxiao",
  events = {fk.CardUsing},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and
      data.card.trueName == "slash"  --and #player.room.discard_pile > 0
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:addPlayerMark(player, "_lm_paoxiao", 1)
    local mark = player:getMark("_lm_paoxiao")
    if mark == 0 or mark % 3 ~= 0 then return end
    local getid
    for _,cid in ipairs(room.discard_pile) do
      local card = Fk:getCardById(cid)
      if card.trueName == "slash" then
        getid = cid
        break
      end
    end
    if getid then
      room:obtainCard(player.id,getid,true,fk.ReasonPrey)
    end
  end,
}
local lm_paoxiao_nolimit = fk.CreateTargetModSkill{
  name = "#lm_paoxiao_nolimit",
  bypass_times = function(self, player, skill, scope)
    if player:hasSkill(self) and skill.trueName == "slash_skill"
      and scope == Player.HistoryPhase then
      return true
    end
  end,
}
local lm__svt_zhangfei = General(extension,"lm__svt_zhangfei","lm_k__wang",0)
lm__svt_zhangfei.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_zhangfei")
lm_paoxiao:addRelatedSkill(lm_paoxiao_nolimit)
lm__svt_zhangfei:addSkill(lm_paoxiao)
Fk:loadTranslationTable{
  ["lm__svt_zhangfei"] = "张飞",
  ["#lm_paoxiao_nolimit"] = "咆哮",
  ["lm_paoxiao"] = "咆哮",
  [":lm_paoxiao"] = "你使用【杀】无次数限制；你每使用三张【杀】时，从弃牌堆获得一张【杀】。",
}
--霍俊
local lm_junshou = fk.CreateTriggerSkill{
  name = "lm_junshou",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local playerhands = player.player_cards[Player.Hand]
    if #playerhands > 0 then
      player:throwAllCards("h")
    end
    room:drawCards(player,3,self.name)
  end,
}
local lm__svt_huojun = General(extension,"lm__svt_huojun","lm_k__wang",0)
lm__svt_huojun.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_huojun")
lm__svt_huojun:addSkill(lm_junshou)
Fk:loadTranslationTable{
  ["lm__svt_huojun"] = "霍峻",
  ["lm_junshou"] = "峻守",
  [":lm_junshou"] = "当你受到伤害后，你可以弃置所有手牌（没牌不弃），然后摸3张牌。",
}
--徐庶
local lm_jiezhen = fk.CreateTriggerSkill{
  name = "lm_jiezhen",
  mute = true,
  events = {fk.TargetSpecified},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.card and data.card.trueName == "slash"
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(data.to)
    data.extra_data = data.extra_data or {}
    data.extra_data.xushujiezhen = data.extra_data.xushujiezhen or {}
    data.extra_data.xushujiezhen = true

    room:setPlayerMark(to,"@@xs_jiezhen_invalid", 1)
  end,
  refresh_events = {fk.CardUseFinished},
  can_refresh = function(self, event, target, player, data)
    return data.extra_data and data.extra_data.xushujiezhen
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    for _, p in pairs(room.alive_players) do
      room:setPlayerMark(p,"@@xs_jiezhen_invalid",0)
    end
  end,
}
local lm_jiezhen_invalid = fk.CreateInvaliditySkill {
  name = "#lm_jiezhen_invalid",
  invalidity_func = function(self, from, skill)
    local mark = from:getMark("@@xs_jiezhen_invalid")
    if mark ~= 0 then
      if skill.attached_equip then
        for _, card in ipairs(Fk.cards) do
          if card.sub_type == Card.SubtypeArmor and skill.attached_equip == card.name then
            return true
          end
        end
      elseif skill:isPlayerSkill(from) and LM.getServantFromSkill(from, skill.name) then
        return true
      end
    end
  end
}
local lm__svt_xushu = General(extension,"lm__svt_xushu","lm_k__wang",0)
lm__svt_xushu.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_xushu")
lm_jiezhen:addRelatedSkill(lm_jiezhen_invalid)
lm__svt_xushu:addSkill(lm_jiezhen)
Fk:loadTranslationTable{
  ["lm__svt_xushu"] = "徐庶",
  ["lm_jiezhen"] = "解阵",
  [":lm_jiezhen"] = "当你使用【杀】指定目标后，其所有将领的技能无效，且防具无效，直到此【杀】结算结束。",
  ["@@xs_jiezhen_invalid"] = "解阵",
}
--魏延
local lm_tunshi = fk.CreateTriggerSkill{
  name = "lm_tunshi",
  events = {fk.CardEffectCancelledOut,fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(self) then return end
    if event == fk.CardEffectCancelledOut then
      return data.from and data.from == player.id and data.card and data.card.trueName == "slash"
    else
      return data.from and data.from == player and not player.dead and data.card and data.card.trueName == "slash" and player:isWounded()
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.CardEffectCancelledOut then
      room:drawCards(player,1,self.name)
    else
      room:recover({
        who = player,
        num = 1,
        recoverBy = player,
        skillName = self.name,
      })
    end
  end,
}
local lm__svt_weiyan = General(extension,"lm__svt_weiyan","lm_k__wang",0)
lm__svt_weiyan.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_weiyan")
lm__svt_weiyan:addSkill(lm_tunshi)
Fk:loadTranslationTable{
  ["lm__svt_weiyan"] = "魏延",
  ["lm_tunshi"] = "吞势",
  [":lm_tunshi"] = "你使用【杀】：被抵消后，你摸一张牌；造成伤害后，你回复1点体力。",
}
--庞统
local lm_manjuan = fk.CreateViewAsSkill{
  name = "lm_manjuan",
  anim_type = "offensive",
  pattern = "analeptic",
  card_filter = function(self, to_select, selected)
    if #selected > 0 then return end
    local card = Fk:getCardById(to_select)
    if card.type ~= Card.TypeBasic and card.color == Card.Red then
      return true
    end
  end,
  view_as = function(self, cards)
    local c = Fk:cloneCard("analeptic")
    c.skillName = self.name
    c:addSubcards(cards)
    return c
  end,
}
local lm__svt_pangtong = General(extension,"lm__svt_pangtong","lm_k__wang",0)
lm__svt_pangtong.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_pangtong")
lm__svt_pangtong:addSkill(lm_manjuan)
Fk:loadTranslationTable{
  ["lm__svt_pangtong"] = "庞统",
  ["lm_manjuan"] = "漫卷",
  [":lm_manjuan"] = "你可以将红色非基本牌当【酒】使用。",
}
--关羽
local lm_nuzhan = fk.CreateTriggerSkill{
  name = "lm_nuzhan",
  anim_type = "offensive",
  events = {fk.CardUseFinished},
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self) and target == player and data.damageDealt and data.card and data.card.trueName == "slash"
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    for pid, _ in pairs(data.damageDealt) do
      local p = room:getPlayerById(pid)
      if p and p ~= player and #(LM.getRemoveableSvts(p) or {}) > 0 then
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    for pid, _ in pairs(data.damageDealt) do
      local p = room:getPlayerById(pid)
      if p and p ~= player and #(LM.getRemoveableSvts(p) or {}) > 0 then
        LM.removeServantsByChc(p, 1,nil,player, nil, self.name)
      end
    end
  end,
}
local lm__svt_guanyu = General(extension,"lm__svt_guanyu","lm_k__wang",0)
lm__svt_guanyu.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_guanyu")
lm__svt_guanyu:addSkill(lm_nuzhan)
Fk:loadTranslationTable{
  ["lm__svt_guanyu"] = "关羽",
  ["lm_nuzhan"] = "怒斩",
  [":lm_nuzhan"] = "你使用【杀】后，你移除所有受到此【杀】伤害的其他角色一名将领。",
}
--马超
local lm_tieqi = fk.CreateTriggerSkill{
  name = "lm_tieqi",
  anim_type = "offensive",
  events = {LM.importedServants},
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return table.find(data.svts, function(svt) return table.contains(LM.getServantSkills(svt), self.name) end)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local getcards,equipsubtypes = {},{ Card.SubtypeWeapon, Card.SubtypeArmor }
    local handleChkCard = function(cards)
      for _, cid in ipairs(cards) do
        local card = Fk:getCardById(cid)
        if card.type == Card.TypeEquip then
          if table.contains(equipsubtypes,card.sub_type) then
            table.insert(getcards,cid)
            table.removeOne(equipsubtypes,card.sub_type)
            if #equipsubtypes == 0 then break end
          end
        end
      end
    end
    handleChkCard(room.draw_pile)
    if #getcards > 0 then
      room:obtainCard(player.id,getcards,true,fk.ReasonJustMove)
    end
  end,
}
local lm__svt_machao = General(extension,"lm__svt_machao","lm_k__wang",0)
lm__svt_machao.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_machao")
lm__svt_machao:addSkill(lm_tieqi)
Fk:loadTranslationTable{
  ["lm__svt_machao"] = "马超",
  ["lm_tieqi"] = "铁骑",
  [":lm_tieqi"] = "你获得此将领后，你从牌堆内获得武器和防具牌各一张。",
}
--简雍
local lm_qiaoshuo = fk.CreateActiveSkill{
  name = "lm_qiaoshuo",
  anim_type = "control",
  prompt = "#lm_qiaoshuo",
  max_phase_use_time = 1,
  card_num = 0,
  card_filter = Util.FalseFunc,
  target_num = 1,
  target_filter = function(self, to_select, selected)
    return #selected == 0 and to_select ~= Self.id and Self:canPindian(Fk:currentRoom():getPlayerById(to_select))
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local to = room:getPlayerById(effect.tos[1])
    local pindian = player:pindian({to}, self.name)
    room:obtainCard(player.id,pindian.results[to.id].toCard,true,fk.ReasonJustMove)
    if pindian.results[to.id].winner ~= player then
      room:obtainCard(to.id,pindian.fromCard,true,fk.ReasonJustMove)
    end
  end,
}
local lm__svt_jianyong = General(extension,"lm__svt_jianyong","lm_k__wang",0)
lm__svt_jianyong.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_jianyong")
lm__svt_jianyong:addSkill(lm_qiaoshuo)
Fk:loadTranslationTable{
  ["lm__svt_jianyong"] = "简雍",
  ["lm_qiaoshuo"] = "巧说",
  [":lm_qiaoshuo"] = "阶段技，你可以与一名其他角色拼点，你获得其拼点牌，若你没赢，其获得你的拼点牌。",
  ["#lm_qiaoshuo"] = "你可以与一名其他角色拼点，你获得其拼点牌，若你没赢，其获得你的拼点牌",
}
--诸葛亮
local lm_miaoji = fk.CreateActiveSkill{
  name = "lm_miaoji",
  anim_type = "control",
  prompt = "#lm_miaoji",
  card_num = 1,
  can_use = function(self, player)
    return table.find(player.player_cards[Player.Hand],function(cid) return Fk:getCardById(cid).type == Card.TypeTrick end)
  end,
  interaction = function()
    local alls = table.filter(LM.getServants(Self), function(svt) return svt.name ~= "lm__svt_zhugeliang" end)
    if #(alls or {}) == 0 then return end
    local choices = {}
    for _, svt in ipairs(alls) do
      if not svt.mark["@lm_miaoji"] then
        table.insert(choices, svt.name)
      end
    end
    return UI.ComboBox {choices = choices, all_choices = table.map(alls, Util.NameMapper)}
  end,
  card_filter = function(self, to_select, selected)
    if #selected > 0 or not self.interaction.data then return false end
    return Fk:getCardById(to_select):isCommonTrick()
  end,
  on_use = function(self, room, effect)
    if not self.interaction.data or self.interaction.data == "" then return end
    local player = room:getPlayerById(effect.from)
    local svt = LM.getServant(player, self.interaction.data)
    if svt then
      local card = Fk:getCardById(effect.cards[1])
      LM.setSvtMark(player, svt.name, "@lm_miaoji", card.name)
      LM.setSvtMark(player, svt.name, "_lm_miaoji", card.id)
      player:addToPile("#lm_miaoji", card, true, self.name)
    end
  end,
}
local lm_miaoji_invalid = fk.CreateInvaliditySkill {
  name = "#lm_miaoji_invalid",
  invalidity_func = function(self, from, skill)
    if not skill:isPlayerSkill(from) then return end
    local servant = LM.getServantFromSkill(from, skill.name)
    return servant and servant.mark["_lm_miaoji"]
  end
}
local lm_miaoji_tri = fk.CreateTriggerSkill{
  name = "#lm_miaoji_tri",
  mute = true,
  refresh_events = {fk.TurnEnd, LM.servantsRemoved},
  can_refresh = function(self, event, target, player, data)
    if player == target then
      if event == LM.servantsRemoved then
        for _, svt in ipairs(data.svts) do
          if svt.mark["_lm_miaoji"] then return true end
        end
      else
        if player:hasSkill(self) then
          for _, svt in ipairs(LM.getServants(player)) do
            if svt.mark["_lm_miaoji"] then return true end
          end
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    if event == LM.servantsRemoved then
      local piles = player:getPile("#lm_miaoji")
      local miaoji = table.filter(data.svts, function(svt) return svt.mark["_lm_miaoji"] end)
      local to_remove = {}
      for _, svt in ipairs(miaoji) do
        if table.contains(piles, svt.mark["_lm_miaoji"]) then
          table.insert(to_remove, svt.mark["_lm_miaoji"])
        end
        LM.setSvtMark(player, svt.name, "@lm_miaoji", nil)
        LM.setSvtMark(player, svt.name, "_lm_miaoji", nil)
      end
      room:moveCardTo(to_remove, Card.DiscardPile, nil, fk.ReasonPutIntoDiscardPile, "lm_miaoji", nil, true)
    elseif event == fk.TurnEnd then
      local piles = player:getPile("#lm_miaoji")
      local miaoji = table.filter(LM.getServants(player), function(svt) return svt.mark["_lm_miaoji"] end)
      local usecards = {}
      for _, svt in ipairs(miaoji) do
        if table.contains(piles, svt.mark["_lm_miaoji"]) then
          table.insert(usecards, svt.mark["@lm_miaoji"])
        end
      end
      for _,cname in ipairs(usecards) do
        U.askForUseVirtualCard(room, player, cname, nil, self.name, nil, true, true, false)
      end
    end
  end,
}
local lm__svt_zhugeliang = General(extension,"lm__svt_zhugeliang","lm_k__wang",0)
lm__svt_zhugeliang.hidden = true
LM.setlordServant(lm_lbjieyi,"lm__svt_zhugeliang")
lm_miaoji:addRelatedSkill(lm_miaoji_invalid)
lm_miaoji:addRelatedSkill(lm_miaoji_tri)
lm__svt_zhugeliang:addSkill(lm_miaoji)
Fk:loadTranslationTable{
  ["lm__svt_zhugeliang"] = "诸葛亮",
  ["lm_miaoji"] = "妙计",
  [":lm_miaoji"] = "出牌阶段，你可以将一张普通锦囊牌移出游戏并于一个无“妙计”标记的其他将领牌上记录之，其所有技能无效。<br>\
  此将领牌被移除后，将“妙计”对应的牌置入弃牌堆。<br>\
  回合结束时，你依次视为使用你将领牌上的“妙计”牌。",
  ["#lm_miaoji"] = "你可以将一张普通锦囊牌移出游戏并于一个无“妙计”标记的其他将领牌上记录之，回合结束时，你依次视为使用你将领牌上的“妙计”牌",
  ["#lm_miaoji_invalid"] = "妙计",
  ["#lm_miaoji_tri"] = "妙计",
  ["@lm_miaoji"] = "妙计",
}

--------------------------------------------------刘宏-------------------------------------------------------------
local lm__liuhong = General(extension,"lm__liuhong","lm_k__wang",5)
local lm_yujue = fk.CreateTriggerSkill{
  name = "lm_yujue",
  mute = true,
  events = {fk.GameStart},
  frequency = Skill.Compulsory,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self)
  end,
  on_use = function(self, event, target, player, data)
    local svts = table.simpleClone(self.lmLordList)
    -- table.shuffle(svts)
    local max = 3
    local num = 1

    svts = table.map(svts, function(s) return LM.initServant(player, s) end)
    LM.setServantsPool(player, svts)
    LM.setServantsMax(player, max)
    local init_svts = table.random(LM.getServantsPool(player), num)
    LM.findServants(player, table.map(init_svts, Util.NameMapper), self.name)
  end
}
local lm_danggu = fk.CreateTriggerSkill{
  name = "lm_danggu",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) then
      return table.find(LM.getServants(player), function(servant) return LM.canFlipServant(servant) end)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local svt = LM.selectServant(player, 1, 1, function(n) return LM.canFlipServant(n) end, self.name, "#lm_danggu-flip")
    if #svt > 0 then
      local another = LM.flipServant(player, svt[1])
      if another then
        LM.removeServants(player, {another.name}, player, nil, self.name)
        local cards = {}
        if #player:getPile("lm_xiyuan") <= 2 then
          cards = player:getPile("lm_xiyuan")
        else
          cards = room:askForCard(player, 2, 2, false, self.name, false, ".|.|.|lm_xiyuan", "#lm_danggu-gain", "lm_xiyuan")
        end
        room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, false, player.id)
      end
    end
  end
}
local lm_xiyuan = fk.CreateActiveSkill{
  name = "lm_xiyuan",
  anim_type = "offensive",
  prompt = "#lm_xiyuan",
  card_num = 2,
  can_use = function(self, player)
    return #player:getCardIds("he") >= 2 and LM.canFindNewSvt(player)
  end,
  card_filter = function(self, to_select, selected)
    return #selected < 2
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local cards = effect.cards
    local max = math.min(LM.getServantSlot(player), 2)
    room:moveCardTo(cards, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
    LM.findServantsByChc(player, 1, max, 3, self.name)
  end
}
lm__liuhong:addSkill(lm_yujue)
lm__liuhong:addSkill(lm_danggu)
lm__liuhong:addSkill(lm_xiyuan)
-- lm__liuhong:addSkill("damage_maker")
-- lm__liuhong:addSkill("control")
-- lm__liuhong.servant = "1/3"
-- LM.setlordServantInfo("lm__liuhong",lm__liuhong.servant)
Fk:loadTranslationTable{
  ["lm__liuhong"] = "刘宏",
  ["@[lm_servants]&lm_yujue"] = "将池",
  ["@[lm_servants]lm_yujue"] = "将领",
  ["lm_yujue"] = "鬻爵",
  [":lm_yujue"] = "军力技，你的初始将领上限为3。游戏开始时，你随机获得一名将领。",
  ["lm_danggu"] = "党锢",
  [":lm_danggu"] = "当你受到伤害后，你将一名将领变为另一状态并移除，然后获得西园两张牌。",
  ["#lm_danggu-flip"] = "党锢：你须将一名将领变为另一状态并移除",
  ["#lm_danggu-gain"] = "党锢：你须获得西园两张牌",
  ["lm_xiyuan"] = "西园",
  [":lm_xiyuan"] = "出牌阶段，你可以将两张牌置入西园，然后发现一次，此次获得至多两名将领(须空位足够)。",
  ["#lm_xiyuan"] = "你可以将两张牌置入西园，然后发现一次，此次获得至多两名将领(须空位足够)",
}

local lm__zhangrang_A = General(extension,"lm__zhangrang_A","lm_k__wang",0)
local lm__zhangrang_B = General(extension,"lm__zhangrang_B","lm_k__wang",0)
local lm_taoluanA = fk.CreateTriggerSkill{
  name = "lm_taoluanA",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.from and data.from ~= player and not data.from:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCardChosen(player, data.from, "he", self.name)
    if card then
      room:moveCardTo(card, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
    end
  end
}
local lm_taoluanB = fk.CreateTriggerSkill{
  name = "lm_taoluanB",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.from and data.from ~= player and not data.from:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askForCardsChosen(player, data.from, 1, 2, "he", self.name)
    if #cards > 0 then
      room:moveCardTo(cards, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
    end
  end
}
lm__zhangrang_A:addSkill(lm_taoluanA)
lm__zhangrang_B:addSkill(lm_taoluanB)
LM.setlordServant(lm_yujue,"lm__zhangrang_A")
lm__zhangrang_A.hidden = true
lm__zhangrang_B.hidden = true
lm__zhangrang_A.trueName = "zhangrang"
lm__zhangrang_B.trueName = "zhangrang"
Fk:loadTranslationTable{
  ["lm__zhangrang_A"] = "张让",
  ["lm_taoluanA"] = "滔乱",
  [":lm_taoluanA"] = "当你受到其他角色造成的伤害后，你可以将其一张牌置入西园。",
  ["lm__zhangrang_B"] = "张让",
  ["lm_taoluanB"] = "滔乱",
  [":lm_taoluanB"] = "当你受到其他角色造成的伤害后，你可以将其至多两张牌置入西园。",
}

local lm__zhaozhong_A = General(extension,"lm__zhaozhong_A","lm_k__wang",0)
local lm__zhaozhong_B = General(extension,"lm__zhaozhong_B","lm_k__wang",0)
local lm_chiyanA = fk.CreateTriggerSkill{
  name = "lm_chiyanA",
  anim_type = "control",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to and data.to ~= player and not data.to:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = room:askForCardChosen(player, data.to, "he", self.name)
    if card then
      room:moveCardTo(card, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
    end
  end
}
local lm_chiyanB = fk.CreateTriggerSkill{
  name = "lm_chiyanB",
  anim_type = "control",
  events = {fk.Damage},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.to and data.to ~= player and not data.to:isNude()
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = room:askForCardsChosen(player, data.to, 1, 2, "he", self.name)
    if #cards > 0 then
      room:moveCardTo(cards, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
    end
  end
}
lm__zhaozhong_A:addSkill(lm_chiyanA)
lm__zhaozhong_B:addSkill(lm_chiyanB)
LM.setlordServant(lm_yujue,"lm__zhaozhong_A")
lm__zhaozhong_A.hidden = true
lm__zhaozhong_B.hidden = true
lm__zhaozhong_A.trueName = "zhaozhong"
lm__zhaozhong_B.trueName = "zhaozhong"
Fk:loadTranslationTable{
  ["lm__zhaozhong_A"] = "赵忠",
  ["lm_chiyanA"] = "鸱鸣",
  [":lm_chiyanA"] = "当你对其他角色造成伤害后，你可以将其一张牌置入西园。",
  ["lm__zhaozhong_B"] = "赵忠",
  ["lm_chiyanB"] = "鸱鸣",
  [":lm_chiyanB"] = "当你对其他角色造成伤害后，你可以将其至多两张牌置入西园。",
}

local lm__xiayun_A = General(extension,"lm__xiayun_A","lm_k__wang",0)
local lm__xiayun_B = General(extension,"lm__xiayun_B","lm_k__wang",0)
local lm_yaozhuoA = fk.CreateTriggerSkill{
  name = "lm_yaozhuoA",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.by_user and data.card.is_damage_card and
    data.card.type == Card.TypeTrick
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end
}
local lm_yaozhuoB = fk.CreateTriggerSkill{
  name = "lm_yaozhuoB",
  anim_type = "offensive",
  events = {fk.DamageCaused},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and data.card and data.by_user and data.card.is_damage_card
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    data.damage = data.damage + 1
  end
}
lm__xiayun_A:addSkill(lm_yaozhuoA)
lm__xiayun_B:addSkill(lm_yaozhuoB)
LM.setlordServant(lm_yujue,"lm__xiayun_A")
lm__xiayun_A.hidden = true
lm__xiayun_B.hidden = true
lm__xiayun_A.trueName = "xiayun"
lm__xiayun_B.trueName = "xiayun"
Fk:loadTranslationTable{
  ["lm__xiayun_A"] = "夏恽",
  ["lm_yaozhuoA"] = "谣诼",
  [":lm_yaozhuoA"] = "你使用伤害类锦囊牌造成伤害时，此次伤害+1。",
  ["lm__xiayun_B"] = "夏恽",
  ["lm_yaozhuoB"] = "谣诼",
  [":lm_yaozhuoB"] = "你使用伤害牌造成伤害时，此次伤害+1。",
}

local lm__guosheng_A = General(extension,"lm__guosheng_A","lm_k__wang",0)
local lm__guosheng_B = General(extension,"lm__guosheng_B","lm_k__wang",0)
local lm_niquA = fk.CreateTriggerSkill{
  name = "lm_niquA",
  anim_type = "offensive",
  events = {fk.EventPhaseProceeding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Finish and #player:getPile("lm_xiyuan") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local _, use = room:askForUseViewAsSkill(player, "#lm_niquA_active", "#lm_niquA-invoke")
    if use and use.interaction then
      self.cost_data = { use.interaction, use.cards, use.targets }
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local name = self.cost_data[1]
    local cards = self.cost_data[2]
    local targets = self.cost_data[3]
    room:useVirtualCard(name, cards, player, table.map(targets, Util.Id2PlayerMapper), self.name)
  end
}
local lm_niquA_active = fk.CreateViewAsSkill{
  name = "#lm_niquA_active",
  pattern = "dismantlement,duel",
  expand_pile = "lm_xiyuan",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "lm_xiyuan"
  end,
  interaction = UI.ComboBox { choices = {"dismantlement", "duel"} },
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = "lm_niquA"
    return card
  end
}
local lm_niquB = fk.CreateViewAsSkill{
  name = "lm_niquB",
  pattern = "dismantlement,duel",
  expand_pile = "lm_xiyuan",
  interaction = function(self)
    local all = {"dismantlement", "duel"}
    local ava = table.filter(all, function(name)
      return Self:getMark("lm_niquB_"..name.."-phase") == 0
    end)
    return UI.ComboBox { choices = ava, all_choices = all}
  end,
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "lm_xiyuan"
  end,
  view_as = function(self, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local name = use.card.name
    player.room:setPlayerMark(player, "lm_niquB_"..name.."-phase", 1)
  end,
  enabled_at_play = function(self, player)
    return #player:getPile("lm_xiyuan") > 0 and player.phase == Player.Play
  end,
  enabled_at_response = function(self, player, response)
    return #player:getPile("lm_xiyuan") > 0 and player.phase == Player.Play and not response
  end
}
lm_niquA:addRelatedSkill(lm_niquA_active)
lm__guosheng_A:addSkill(lm_niquA)
lm__guosheng_B:addSkill(lm_niquB)
LM.setlordServant(lm_yujue,"lm__guosheng_A")
lm__guosheng_A.hidden = true
lm__guosheng_B.hidden = true
lm__guosheng_A.trueName = "guosheng"
lm__guosheng_B.trueName = "guosheng"
Fk:loadTranslationTable{
  ["lm__guosheng_A"] = "郭胜",
  ["lm_niquA"] = "逆取",
  [":lm_niquA"] = "结束阶段，你可以将西园的一张牌当【过河拆桥】或【决斗】使用。",
  ["#lm_niquA_active"] = "逆取",
  ["#lm_niquA-invoke"] = "逆取：你可以将西园的一张牌当【过河拆桥】或【决斗】使用",
  ["lm__guosheng_B"] = "郭胜",
  ["lm_niquB"] = "逆取",
  [":lm_niquB"] = "出牌阶段各限一次，你可以将西园的一张牌当【过河拆桥】或【决斗】使用。",
}

local lm__sunzhang_A = General(extension,"lm__sunzhang_A","lm_k__wang",0)
local lm__sunzhang_B = General(extension,"lm__sunzhang_B","lm_k__wang",0)
local lm_zimouA = fk.CreateTriggerSkill{
  name = "lm_zimouA",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and #player:getPile("lm_xiyuan") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("lm_xiyuan")
    local cids = room:askForCardsChosen(target, player, 1, 2, {card_data = {{"lm_xiyuan", player:getPile("lm_xiyuan")}}}, self.name, "#lm_zimouA-ask")
    if #cids ~= 0 then
      room:moveCardTo(cids, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, false, player.id)
    end
  end
}
local lm_zimouB = fk.CreateTriggerSkill{
  name = "lm_zimouB",
  anim_type = "drawcard",
  events = {fk.TurnEnd},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and #player:getPile("lm_xiyuan") > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cards = player:getPile("lm_xiyuan")
    room:moveCardTo(cards, Card.PlayerHand, player, fk.ReasonJustMove, self.name, nil, false, player.id)
    player:drawCards(1, self.name)
  end
}
lm__sunzhang_A:addSkill(lm_zimouA)
lm__sunzhang_B:addSkill(lm_zimouB)
LM.setlordServant(lm_yujue,"lm__sunzhang_A")
lm__sunzhang_A.hidden = true
lm__sunzhang_B.hidden = true
lm__sunzhang_A.trueName = "sunzhang"
lm__sunzhang_B.trueName = "sunzhang"
Fk:loadTranslationTable{
  ["lm__sunzhang_A"] = "孙璋",
  ["lm_zimouA"] = "自谋",
  [":lm_zimouA"] = "回合结束时，你可以获得西园的至多两张牌。",
  ["#lm_zimouA-ask"] = "自谋：你可以获得西园的至多两张牌。",
  ["lm__sunzhang_B"] = "孙璋",
  ["lm_zimouB"] = "自谋",
  [":lm_zimouB"] = "回合结束时，你可以获得西园的所有牌并摸一张牌。",
}

local lm__bilan_A = General(extension,"lm__bilan_A","lm_k__wang",0)
local lm__bilan_B = General(extension,"lm__bilan_B","lm_k__wang",0)
local lm_picaiA = fk.CreateTriggerSkill{
  name = "lm_picaiA",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(1, self.name)
  end
}
local lm_picaiB = fk.CreateTriggerSkill{
  name = "lm_picaiB",
  anim_type = "masochism",
  events = {fk.Damaged},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self)
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:drawCards(2, self.name)
    if not player:isKongcheng() then
      local cards = room:askForCard(player, 1, 1, true, self.name, false, ".", "#lm_picaiB-put")
      if #cards > 0 then
        room:moveCardTo(cards, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
      end
    end
  end
}
local lm_picai_maxcard = fk.CreateMaxCardsSkill{
  name = "#lm_picai_maxcard",
  correct_func = function(self, player)
    return (player:hasSkill(lm_picaiA) or player:hasSkill(lm_picaiB)) and #player:getPile("lm_xiyuan")
  end
}
lm_picaiA:addRelatedSkill(lm_picai_maxcard)
-- lm_picaiB:addRelatedSkill(lm_picai_maxcard)
lm__bilan_A:addSkill(lm_picaiA)
lm__bilan_B:addSkill(lm_picaiB)
LM.setlordServant(lm_yujue,"lm__bilan_A")
lm__bilan_A.hidden = true
lm__bilan_B.hidden = true
lm__bilan_A.trueName = "bilan"
lm__bilan_B.trueName = "bilan"
Fk:loadTranslationTable{
  ["lm__bilan_A"] = "毕岚",
  ["lm_picaiA"] = "庀材",
  [":lm_picaiA"] = "你的手牌上限+X（X为西园牌数）。当你受到伤害后，你摸一张牌。",
  ["lm__bilan_B"] = "毕岚",
  ["lm_picaiB"] = "庀材",
  [":lm_picaiB"] = "你的手牌上限+X（X为西园牌数）。当你受到伤害后，你摸两张牌，然后将一张牌置入西园。",
  ["#lm_picai_maxcard"] = "庀材",
  ["#lm_picaiB-put"] = "庀材：你须将一张牌置入西园",
}

local lm__lisong_A = General(extension,"lm__lisong_A","lm_k__wang",0)
local lm__lisong_B = General(extension,"lm__lisong_B","lm_k__wang",0)
local lm_kuijiA = fk.CreateActiveSkill{
  name = "lm_kuijiA",
  anim_type = "control",
  prompt = "#lm_kuijiA",
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local xiyuan = player:getPile("lm_xiyuan")
    local target_hands = target:getCardIds("h")
    local cards = room:askForPoxi(player, "lm_kuiji_discard", {
      { "lm_xiyuan", xiyuan },
      { target.general, target_hands },
    }, nil, true)
    if #cards == 0 then return end
    local cards1 = table.filter(cards, function(id) return table.contains(xiyuan, id) end)
    local cards2 = table.filter(cards, function(id) return table.contains(target_hands, id) end)
    local moveInfos = {}
    if #cards1 > 0 then
      table.insert(moveInfos, {
        from = player.id,
        ids = cards1,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = self.name,
      })
    end
    if #cards2 > 0 then
      table.insert(moveInfos, {
        from = target.id,
        ids = cards2,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = self.name,
      })
    end
    room:moveCards(table.unpack(moveInfos))
  end,
}
local lm_kuijiB = fk.CreateActiveSkill{
  name = "lm_kuijiB",
  anim_type = "control",
  prompt = "#lm_kuijiB",
  max_phase_use_time = 1,
  card_num = 0,
  target_num = 1,
  card_filter = Util.FalseFunc,
  target_filter = function(self, to_select, selected, selected_cards)
    return #selected == 0 and to_select ~= Self.id and not Fk:currentRoom():getPlayerById(to_select):isKongcheng()
  end,
  on_use = function(self, room, effect)
    local player = room:getPlayerById(effect.from)
    local target = room:getPlayerById(effect.tos[1])
    local xiyuan = player:getPile("lm_xiyuan")
    local target_hands = target:getCardIds("h")
    local cards = room:askForPoxi(player, "lm_kuiji_discard", {
      { "lm_xiyuan", xiyuan },
      { target.general, target_hands },
    }, { svtstate = true }, true)
    if #cards == 0 then return end
    local cards1 = table.filter(cards, function(id) return table.contains(xiyuan, id) end)
    local cards2 = table.filter(cards, function(id) return table.contains(target_hands, id) end)
    local moveInfos = {}
    if #cards1 > 0 then
      table.insert(moveInfos, {
        from = player.id,
        ids = cards1,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = self.name,
      })
    end
    if #cards2 > 0 then
      table.insert(moveInfos, {
        from = target.id,
        ids = cards2,
        toArea = Card.DiscardPile,
        moveReason = fk.ReasonDiscard,
        proposer = effect.from,
        skillName = self.name,
      })
    end
    room:moveCards(table.unpack(moveInfos))
  end,
}
Fk:addPoxiMethod{
  name = "lm_kuiji_discard",
  card_filter = function(to_select, selected, data)
    local suit = Fk:getCardById(to_select).suit
    if suit == Card.NoSuit then return false end
    return not table.find(selected, function(id) return Fk:getCardById(id).suit == suit end)
  end,
  feasible = function(selected, _, extra_data)
    if extra_data and extra_data.svtstate then
      return #selected == 4 or #selected == 3
    end
    return #selected == 4
  end,
  prompt = function(_, extra_data)
    if extra_data and extra_data.svtstate then
      return "#lm_kuijiB-poxi_prompt"
    end
    return "#lm_kuijiA-poxi_prompt"
  end
}
lm__lisong_A:addSkill(lm_kuijiA)
lm__lisong_B:addSkill(lm_kuijiB)
LM.setlordServant(lm_yujue,"lm__lisong_A")
lm__lisong_A.hidden = true
lm__lisong_B.hidden = true
lm__lisong_A.trueName = "lisong"
lm__lisong_B.trueName = "lisong"
Fk:loadTranslationTable{
  ["lm__lisong_A"] = "栗嵩",
  ["lm_kuijiA"] = "窥机",
  [":lm_kuijiA"] = "阶段技，你可以观看一名其他角色所有手牌，然后可以弃置其手牌与西园的牌中共计四张花色不同的牌。",
  ["#lm_kuijiA"] = "你可以观看一名其他角色所有手牌，然后可以弃置其手牌与西园的牌中共计四张花色不同的牌",
  ["#lm_kuijiA-poxi_prompt"] = "窥机：你可以弃置其手牌与西园的牌中共计四张花色不同的牌",
  ["lm__lisong_B"] = "栗嵩",
  ["lm_kuijiB"] = "窥机",
  [":lm_kuijiB"] = "阶段技，你可以观看一名其他角色所有手牌，然后可以弃置其手牌与西园的牌中共计三张或四张花色不同的牌。",
  ["#lm_kuijiB"] = "你可以观看一名其他角色所有手牌，然后可以弃置其手牌与西园的牌中共计三张或四张花色不同的牌",
  ["#lm_kuijiB-poxi_prompt"] = "窥机：你可以弃置其手牌与西园的牌中共计三张或四张花色不同的牌",
}

local lm__duangui_A = General(extension,"lm__duangui_A","lm_k__wang",0)
local lm__duangui_B = General(extension,"lm__duangui_B","lm_k__wang",0)
local lm_chiheA = fk.CreateTriggerSkill{
  name = "lm_chiheA",
  anim_type = "control",
  events = {fk.EventPhaseProceeding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(self) and player.phase == Player.Start
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = room:getOtherPlayers(player)
    if #targets < 1 then return end
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lm_chiheA-invoke", self.name)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    to:drawCards(2, self.name)
    if not to:isKongcheng() then
      local cards = room:askForCard(to, 2, 2, true, self.name, false, ".", "#lm_chihe-give::" .. player.id)
      if #cards > 0 then
        room:obtainCard(player, cards, false, fk.ReasonGive, to.id, self.name)
      end
    end
    if not player:isKongcheng() then
      local cards = room:askForCard(player, 1, 1, true, self.name, false, ".", "#lm_chihe-put")
      if #cards > 0 then
        room:moveCardTo(cards, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
      end
    end
    -- room:setPlayerMark(player, "@lm_chihe", to.general)
    room:setPlayerMark(player, "@[chara]lm_chihe-turn", to.id)
  end
}
local lm_chiheB = fk.CreateTriggerSkill{
  name = "lm_chiheB",
  anim_type = "control",
  events = {fk.EventPhaseProceeding},
  can_trigger = function(self, event, target, player, data)
    return target == player and player.phase == Player.Start and player:hasSkill(self) and #player:getPile("lm_xiyuan") > 0
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function(p) return #p:getCardIds("he") >= 2 end)
    if #targets < 1 then return end
    local to = room:askForChoosePlayers(player, table.map(targets, Util.IdMapper), 1, 1, "#lm_chiheB-invoke", self.name)
    if #to > 0 then
      self.cost_data = to[1]
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = room:getPlayerById(self.cost_data)
    if not to:isKongcheng() then
      local cards = room:askForCard(to, 2, 2, true, self.name, false, ".", "#lm_chihe-give::" .. player.id)
      if #cards > 0 then
        room:obtainCard(player, cards, false, fk.ReasonGive, to.id, self.name)
      end
    end
    if not player:isKongcheng() then
      local cards = room:askForCard(player, 1, 1, true, self.name, false, ".", "#lm_chihe-put")
      if #cards > 0 then
        room:moveCardTo(cards, Player.Special, player, fk.ReasonPut, self.name, "lm_xiyuan", false, player.id)
      end
    end
    -- room:setPlayerMark(player, "@lm_chihe-turn", to.general)
    room:setPlayerMark(player, "@[chara]lm_chihe-turn", to.id)
  end
}
local lm_chihe_prohibit = fk.CreateProhibitSkill{
  name = "#lm_chihe_prohibit",
  is_prohibited = function(self, from, to, card)
    if from and from:getMark("@[chara]lm_chihe-turn") ~= 0 then
      return to.id == from:getMark("@[chara]lm_chihe-turn")
    end
  end
}
lm_chiheA:addRelatedSkill(lm_chihe_prohibit)
-- lm_chiheB:addRelatedSkill(lm_chihe_prohibit)
lm__duangui_A:addSkill(lm_chiheA)
lm__duangui_B:addSkill(lm_chiheB)
LM.setlordServant(lm_yujue,"lm__duangui_A")
lm__duangui_A.hidden = true
lm__duangui_B.hidden = true
lm__duangui_A.trueName = "duangui"
lm__duangui_B.trueName = "duangui"
Fk:loadTranslationTable{
  ["lm__duangui_A"] = "段珪",
  ["lm_chiheA"] = "叱喝",
  [":lm_chiheA"] = "准备阶段，你可以令一名其他角色摸并交给你两张牌，你将一张牌置入西园，然后本回合你不能对其使用牌。",
  ["#lm_chiheA-invoke"] = "叱喝：你可以令一名其他角色摸并交给你两张牌，你将一张牌置入西园，然后本回合你不能对其使用牌",
  ["lm__duangui_B"] = "段珪",
  ["lm_chiheB"] = "叱喝",
  [":lm_chiheB"] = "准备阶段，你可以令一名其他角色交给你两张牌，你将一张牌置入西园，然后本回合你不能对其使用牌。",
  ["#lm_chiheB-invoke"] = "叱喝：你可以令一名其他角色交给你两张牌，你将一张牌置入西园，然后本回合你不能对其使用牌",
  ["#lm_chihe_prohibit"] = "叱喝",
  ["#lm_chihe-give"] = "叱喝：你须交给 %dest 两张牌",
  ["#lm_chihe-put"] = "叱喝：你须将一张牌置入西园",
  ["@[chara]lm_chihe-turn"] = "叱喝",
}

local lm__gaowang_A = General(extension,"lm__gaowang_A","lm_k__wang",0)
local lm__gaowang_B = General(extension,"lm__gaowang_B","lm_k__wang",0)
local lm_anruoA = fk.CreateViewAsSkill{
  name = "lm_anruoA",
  pattern = "peach",
  expand_pile = "lm_xiyuan",
  card_filter = function(self, to_select, selected)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "lm_xiyuan" and Fk:getCardById(to_select).suit == Card.Heart
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local card = Fk:cloneCard("peach")
    card:addSubcards(cards)
    card.skillName = self.name
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local logic = room.logic
    local current_event = logic:getCurrentEvent()
    local allm_turn_events = logic.event_recorder[GameEvent.Turn]
    local last = {}
    if type(allm_turn_events) == "table" then
      local index = #allm_turn_events
      if index > 0 then
        local turn_event = current_event:findParent(GameEvent.Turn)
        if turn_event ~= nil then
          index = index - 1
        end
        if index > 0 then
          current_event = allm_turn_events[index]
          last = current_event:searchEvents(GameEvent.UseCard, 1, function(e)
            return table.contains(e.data[1].card.skillNames, "lm_anruoA")
          end)
        end
      end
    end
    if #last == 0 then
      player:drawCards(1, "lm_anruoA")
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getPile("lm_xiyuan") > 0
  end,
  enabled_at_response = function(self, player, response)
    return #player:getPile("lm_xiyuan") > 0 and not response
  end
}
local lm_anruoB = fk.CreateViewAsSkill{
  name = "lm_anruoB",
  pattern = "peach,analeptic",
  expand_pile = "lm_xiyuan",
  card_filter = function(self, to_select, selected)
    local card = Fk:getCardById(to_select)
    return #selected == 0 and Self:getPileNameOfId(to_select) == "lm_xiyuan" and
    (card.suit == Card.Heart or card.suit == Card.Spade)
  end,
  view_as = function(self, cards)
    if #cards ~= 1 then return end
    local src = Fk:getCardById(cards[1])
    local name = "peach"
    if src.suit == Card.Spade then
      name = "analeptic"
    end
    local card = Fk:cloneCard(name)
    card:addSubcards(cards)
    card.skillName = "lm_anruoA"
    return card
  end,
  before_use = function(self, player, use)
    local room = player.room
    local logic = room.logic
    local current_event = logic:getCurrentEvent()
    local allm_turn_events = logic.event_recorder[GameEvent.Turn]
    local last = {}
    if type(allm_turn_events) == "table" then
      local index = #allm_turn_events
      if index > 0 then
        local turn_event = current_event:findParent(GameEvent.Turn)
        if turn_event ~= nil then
          index = index - 1
        end
        if index > 0 then
          current_event = allm_turn_events[index]
          last = current_event:searchEvents(GameEvent.UseCard, 1, function(e)
            local c = e.data[1].card
            return c and c.name == use.card.name and table.contains(e.data[1].card.skillNames, "lm_anruoA")
          end)
        end
      end
    end
    if #last == 0 then
      player:drawCards(1, "lm_anruoB")
    end
  end,
  enabled_at_play = function(self, player)
    return #player:getPile("lm_xiyuan") > 0
  end,
  enabled_at_response = function(self, player, response)
    return #player:getPile("lm_xiyuan") > 0 and not response
  end
}
lm__gaowang_A:addSkill(lm_anruoA)
lm__gaowang_B:addSkill(lm_anruoB)
LM.setlordServant(lm_yujue,"lm__gaowang_A")
lm__gaowang_A.hidden = true
lm__gaowang_B.hidden = true
lm__gaowang_A.trueName = "gaowang"
lm__gaowang_B.trueName = "gaowang"
Fk:loadTranslationTable{
  ["lm__gaowang_A"] = "高望",
  ["lm_anruoA"] = "安弱",
  [":lm_anruoA"] = "你可以将西园的红桃牌当【桃】使用。若未连续两回合转化，你摸一张牌。",
  ["lm__gaowang_B"] = "高望",
  ["lm_anruoB"] = "安弱",
  [":lm_anruoB"] = "你可以将西园的红桃牌当【桃】、黑桃牌当【酒】使用，若未连续两回合转化同一种牌，你摸一张牌。",
}

local lm__hankui_A = General(extension,"lm__hankui_A","lm_k__wang",0)
local lm__hankui_B = General(extension,"lm__hankui_B","lm_k__wang",0)
local lm_xiaoluA = fk.CreateTriggerSkill{
  name = "lm_xiaoluA",
  anim_type = "control",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target and target ~= player and player:hasSkill(self) and #player:getPile("lm_xiyuan") >= 1
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#lm_xiaoluA-invoke:" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cid = room:askForCardChosen(target, player, {card_data = {{"lm_xiyuan", player:getPile("lm_xiyuan")}}}, "lm_xiyuan")
    room:throwCard(cid, self.name, player, target)
    local card = Fk:getCardById(cid)
    room:setPlayerMark(target, "@lm_xiaolu-turn", Fk:translate("log_" .. card:getColorString()))
    room:setPlayerMark(target, "_lm_xiaolu-turn", {card.color})
  end
}
local lm_xiaoluB = fk.CreateTriggerSkill{
  name = "lm_xiaoluB",
  anim_type = "drawcard",
  events = {fk.TurnStart},
  can_trigger = function(self, event, target, player, data)
    return target and target ~= player and player:hasSkill(self) and #player:getPile("lm_xiyuan") >= 2
  end,
  on_cost = function(self, event, target, player, data)
    return player.room:askForSkillInvoke(player, self.name, nil, "#lm_xiaoluB-invoke:" .. target.id)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local cids = room:askForCardsChosen(target, player, 2, 2, {card_data = {{"lm_xiyuan", player:getPile("lm_xiyuan")}}}, "lm_xiyuan")
    room:throwCard(cids, self.name, player, target)
    local csr = {}
    local car = {}
    for _, cid in ipairs(cids) do
      local card = Fk:getCardById(cid)
      table.insertIfNeed(csr, Fk:translate("log_" .. card:getColorString()))
      table.insertIfNeed(car, card.color)
    end
    room:setPlayerMark(target, "@lm_xiaolu-turn", table.concat(csr, ""))
    room:setPlayerMark(target, "_lm_xiaolu-turn", car)
  end
}
local lm_xiaolu_prohibit = fk.CreateProhibitSkill{
  name = "#lm_xiaolu_prohibit",
  prohibit_use = function(self, player, card)
    if player and #player:getTableMark("_lm_xiaolu-turn") > 0 then
      return table.contains(player:getTableMark("_lm_xiaolu-turn"), card.color)
    end
  end
}
lm_xiaoluA:addRelatedSkill(lm_xiaolu_prohibit)
-- lm_xiaoluB:addRelatedSkill(lm_xiaolu_prohibit)
lm__hankui_A:addSkill(lm_xiaoluA)
lm__hankui_B:addSkill(lm_xiaoluB)
LM.setlordServant(lm_yujue,"lm__hankui_A")
lm__hankui_A.hidden = true
lm__hankui_B.hidden = true
lm__hankui_A.trueName = "hankui"
lm__hankui_B.trueName = "hankui"
Fk:loadTranslationTable{
  ["lm__hankui_A"] = "韩悝",
  ["lm_xiaoluA"] = "宵赂",
  [":lm_xiaoluA"] = "其他角色回合开始时，你可以令其弃置西园一张牌，本回合其不能使用该颜色的牌。",
  ["#lm_xiaoluA-invoke"] = "宵赂：你可以令 %src 弃置西园一张牌，本回合其不能使用该颜色的牌",
  ["lm__hankui_B"] = "韩悝",
  ["lm_xiaoluB"] = "宵赂",
  [":lm_xiaoluB"] = "其他角色回合开始时，你可以令其弃置西园两张牌，本回合其不能使用该颜色的牌。",
  ["#lm_xiaoluB-invoke"] = "宵赂：你可以令 %src 弃置西园两张牌，本回合其不能使用该颜色的牌",
  ["@lm_xiaolu-turn"] = "宵赂",
  ["#lm_xiaolu_prohibit"] = "宵赂",
  ["log_black"] = '黑',
  ["log_red"] = '<font color="#CC3131">红</font>',
  ["log_nocolor"] = '<font color="grey">无</font>',
}

return extension